
from __future__ import with_statement

import os
import time
from hashlib import sha1

from openlookup.engine.item import create_multivalued_item
from openlookup.engine.db import DB

RUN_TIME = 60
TTL = 300

data = 'x' * 1024
salt = os.urandom(10)

def key(i):
    return salt + '\0' + str(i)

db = DB('openlookup.db', fast=True)

start = last_pause = time.time()

k = 0
while True:
    now = time.time()
    if now - start > RUN_TIME:
        break

    # Give the daemon a chance to run:
    if now - last_pause > 10:
        last_pause = now
        time.sleep(2)
    else:
        time.sleep(0.2)

    with db.transaction():
        for j in range(400):
            k += 1
            key_hash = sha1(key(k)).hexdigest()
            value_hash = sha1(data).hexdigest()
            item = create_multivalued_item(key_hash, value_hash, '',
                                           value=data, ttl=TTL)
            item.validate()
            db.put(item, priority=1)
